﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCDL.GLOBAL;
using TCDL.BUS;

namespace TCDL
{
    public partial class frmDangNhap : Form
    {
        public frmDangNhap()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            string strDB = "ETransport";
            if (rbLoi.Checked) 
            {
                strDB = "ETransportError";
            }
            User.strConnect = @"Data Source=" + txtChuoiKetNoi.Text.Trim()
                + ";Initial Catalog=" + strDB
                + ";Integrated Security=True;user=" + txtUser.Text.Trim()
                + ";password=" + txtPass.Text.Trim();

            int iCount = 2;
            gConnect[] gCon = new gConnect[iCount];
            String strTenDN = txtNguoiDung.Text;
            String strMatKhau = txtMatKhau.Text;

            gCon[0] = new gConnect("@username", strTenDN);
            gCon[1] = new gConnect("@password", strMatKhau);
            Object obj = new XemDSBUS().XemDS("sp_DangNhap", gCon, iCount);
            if(obj == null)
            {
                MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu");
                return;
            }
            else
            {
                DataTable db = (DataTable) obj;
                User.user = obj;
                String t = db.Rows[0][db.Columns.Count - 1].ToString();
                gCon = new gConnect[1];
                if (t.Equals("1"))
                {
                    gCon[0] = new gConnect("@MaKH", db.Columns[0].ToString());
                    new frmChinh_TimKiem("1", gCon, strTenDN).Show();
                }
                else
                {
                    gCon[0] = new gConnect("@MaNV", db.Columns[0].ToString());
                    if (strTenDN.Trim() == "admin")
                    {
                        new frmAdmin().Show();
                    }
                    else
                    {
                        new frmChinh_TimKiem("2", gCon, strTenDN).Show();
                    }
                }
            }
        }
    }
}
